{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Check if Binary String Has at Most One Segment of Ones"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: checkOnesSegment"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #检查二进制字符串字段"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个二进制字符串 <code>s</code> ，该字符串 <strong>不含前导零</strong> 。</p>\n",
    "\n",
    "<p>如果 <code>s</code> 包含 <strong>零个或一个由连续的 <code>'1'</code> 组成的字段</strong> ，返回 <code>true</code>​​​ 。否则，返回 <code>false</code> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"1001\"\n",
    "<strong>输出：</strong>false\n",
    "<strong>解释：</strong>由连续若干个&nbsp;<code>'1'</code> 组成的字段数量为 2，返回 false\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"110\"\n",
    "<strong>输出：</strong>true</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= s.length &lt;= 100</code></li>\n",
    "\t<li><code>s[i]</code>​​​​ 为 <code>'0'</code> 或 <code>'1'</code></li>\n",
    "\t<li><code>s[0]</code> 为 <code>'1'</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [check-if-binary-string-has-at-most-one-segment-of-ones](https://leetcode.cn/problems/check-if-binary-string-has-at-most-one-segment-of-ones/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [check-if-binary-string-has-at-most-one-segment-of-ones](https://leetcode.cn/problems/check-if-binary-string-has-at-most-one-segment-of-ones/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"1001\"', '\"110\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        return '1'*s.count('1') in s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        n = len(s)\n",
    "        count = 1\n",
    "        i = 1\n",
    "        while i < n:\n",
    "            if s[i] == '1' and s[i-1] == '1':\n",
    "                i += 1\n",
    "                continue\n",
    "            elif s[i] == '1' and s[i-1] == '0':\n",
    "                i += 1\n",
    "                count += 1\n",
    "            else:\n",
    "                i += 1\n",
    "            if count > 1:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        flag = 0\n",
    "        for c in s:\n",
    "            if c == \"0\":\n",
    "                flag = 1\n",
    "            if flag and c == \"1\":\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        return '1'*s.count('1') in s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        return \"01\" not in s\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        return '01' not in s\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import re\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        return len(re.findall(r'1+',s)) <= 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        ls = s.split('0')\n",
    "        ls = [item for item in ls if item != '']\n",
    "        if(len(ls) > 1):\n",
    "            return False\n",
    "        return True;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        cnt = 0\n",
    "        for i in range(1,len(s)):\n",
    "            if s[i] != s[i-1]:\n",
    "                cnt += 1\n",
    "        return cnt <= 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        i = 1\n",
    "        res = 0\n",
    "        if s[0] == '1':\n",
    "            res = 1\n",
    "        while i<len(s):\n",
    "            if s[i] == '1' and s[i-1] == '0':\n",
    "                res += 1\n",
    "            if res >=2:\n",
    "                return False\n",
    "            i += 1\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        flag=True\n",
    "        if '0' in s:\n",
    "            idx=s.index('0')\n",
    "        else:\n",
    "            return True\n",
    "        for i in range(idx,len(s)):\n",
    "            if s[i]=='1':\n",
    "                flag=False\n",
    "        return flag"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        if s.count(\"1\") <= 1:\n",
    "            return True\n",
    "        ss = s.split(\"0\")\n",
    "        return len([i for i in ss if i]) <= 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        n = len(s)\n",
    "        j = n-1\n",
    "        for i in range(1,n):\n",
    "            if s[i] != s[i-1]:\n",
    "                j = i\n",
    "                break\n",
    "        if j == n-1:\n",
    "            return True\n",
    "        if str(1) in s[j:]:\n",
    "            return False\n",
    "        else:\n",
    "            return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    \n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        n = len(s) \n",
    "        start, end = 0, n - 1\n",
    "\n",
    "        while end > - 1 and s[end] == '0':\n",
    "            end -= 1\n",
    "\n",
    "        if end < 0:\n",
    "            return False        \n",
    "\n",
    "        while start < n and s[start] == '0':\n",
    "            start += 1\n",
    "\n",
    "        if start == n:\n",
    "            return False\n",
    "\n",
    "\n",
    "        return '0' not in s[start: end]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        return '01' not in s\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        return '01' not in s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        if s.count('1') == 1:\n",
    "            return True\n",
    "\n",
    "\n",
    "        for i in range(len(s) - 1):\n",
    "            if i + 2 < len(s) and s[i] == '1' and s[i + 1] == '0' and '1' in s[i + 2:]:\n",
    "                return False \n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        if s.count('1')==0 or s.count('1')==1:\n",
    "            return True\n",
    "        c=0\n",
    "        for i in range(len(s)-1):\n",
    "            if s[i]=='1' and s[i+1]=='1':\n",
    "                c+=1\n",
    "        return c+1==s.count('1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        return \"01\" not in s\n",
    "        # if len(s)==1:\n",
    "        #     return True\n",
    "        \n",
    "        # else:\n",
    "        #     if len(s)==2:\n",
    "        #         return True\n",
    "\n",
    "\n",
    "        #     else:\n",
    "        #         if s[1] == '1': #等于1则计算长度是否为2  \n",
    "        #             for j in range(2,len(s)):  #2开始（表示位置3），表明len（s）得》3\n",
    "        #                 if s[j] == '1':\n",
    "        #                     return False \n",
    "        #                 else:\n",
    "        #                     return True\n",
    "\n",
    "        #         else: #等于0则查看后续是否有1\n",
    "        #             for i in range(2,len(s)):  \n",
    "        #                 if s[i] == '1':\n",
    "        #                     return True\n",
    "        #                 else:\n",
    "        #                     return False\n",
    "\n",
    "\n",
    "# 重点在于题目的理解，1与1之间不能有0\n",
    "# 有0则错误。\n",
    "# 另外要注意字符串与数字1的区别\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def checkOnesSegment(self, s: str) -> bool:\r\n",
    "        if '0' not in s:\r\n",
    "            return True\r\n",
    "        else:\r\n",
    "            a = s.split('0')\r\n",
    "            return len(a) - a.count('') == 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        idx,ans = 0,0\n",
    "        for i in range(1,len(s)):\n",
    "            if s[i] == '0' and s[idx] == '1':\n",
    "                ans += 1\n",
    "                idx = i\n",
    "            elif s[i] == '1' and s[idx] == '0':\n",
    "                idx = i \n",
    "        if s[-1] == '1':\n",
    "            ans += 1\n",
    "        return ans <= 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        return not s.count(\"01\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    \n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        n = len(s) \n",
    "        start, end = 0, n - 1\n",
    "        pre, bck = '', ''\n",
    "\n",
    "\n",
    "        while end > - 1 and s[end] == '0':\n",
    "            end -= 1\n",
    "\n",
    "        if end < 0:\n",
    "            return False        \n",
    "\n",
    "        while start < n and s[start] == '0':\n",
    "            start += 1\n",
    "\n",
    "        if start == n:\n",
    "            return False\n",
    "\n",
    "\n",
    "        while start < end:\n",
    "\n",
    "            pre += s[start]\n",
    "            bck = s[end] + bck\n",
    "\n",
    "   \n",
    "\n",
    "            if  '1' in pre and '01' in bck:\n",
    "                return False\n",
    "            \n",
    "            if '10' in pre and '1' in bck:\n",
    "                return False\n",
    "\n",
    "            start += 1\n",
    "            end -= 1\n",
    "        \n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        string = list(filter(None, s.split(\"0\")))\n",
    "        x = len(string)\n",
    "        return x == 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        n = s.count('1') \n",
    "        if n < 2:\n",
    "            return True\n",
    "        first = s.index('1')\n",
    "        return s[first: first + n] == '1' * n\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        return \"01\" not in s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:   \n",
    "        return s.count(\"1\") <= 1 or len([i for i in s.split(\"0\") if i]) <= 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        flag = False\n",
    "        cnt = 0\n",
    "        for ch in s:\n",
    "            if ch == '1':\n",
    "                if not flag:\n",
    "                    cnt += 1\n",
    "                    if cnt > 1:\n",
    "                        return False\n",
    "                    flag = True\n",
    "            else:\n",
    "                flag = False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        return '01' not in s\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        return Counter(s)[\"1\"] * \"1\" in s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "\n",
    "        return '01' not in s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        n=len(s)\n",
    "        if '0'==s:\n",
    "            return True\n",
    "        m=s.count('1')\n",
    "        if '1'*m+'0'*(n-m)==s:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        i = ans = 0\n",
    "        n = len(s)\n",
    "        while i < n and s[i] == \"1\":\n",
    "            i += 1\n",
    "        while i < n:\n",
    "            if s[i] == \"1\":\n",
    "                return False\n",
    "            i += 1\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        return \"01\" not in s\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        count = 0\n",
    "        cur = s[0]\n",
    "        if cur == '1': count+=1\n",
    "        for i in range(1, len(s)):            \n",
    "            # print(cur)\n",
    "            if s[i] != cur: \n",
    "                if s[i] == '1':\n",
    "                    count+=1\n",
    "                cur = s[i]\n",
    "            if count > 1: return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        cnt = 0\n",
    "        i = 0\n",
    "        isfalg = True\n",
    "        while(i < len(s)):\n",
    "            if s[i] == '1':\n",
    "                if isfalg:\n",
    "                    cnt += 1\n",
    "                    isfalg = False\n",
    "            else:\n",
    "                isfalg = True\n",
    "            i += 1\n",
    "        return cnt == 1 or cnt == 0\n",
    "            \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        # cnt = 0\n",
    "        # flag = 1\n",
    "        # for i in s:\n",
    "        #     if i == '1':\n",
    "        #         cnt += flag \n",
    "        #         flag = 0\n",
    "        #     else:\n",
    "        #         flag = 1\n",
    "        # return cnt in (0, 1)\n",
    "\n",
    "        # -------------------------\n",
    "        # return True if (idx := s.find('0')) < 0 else '1' not in s[idx:]\n",
    "\n",
    "        # --------------------\n",
    "        return (s+'0').count('10') == 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        try:\n",
    "            s.index('0')\n",
    "            a=True\n",
    "        except ValueError:\n",
    "            a=False\n",
    "        if a:\n",
    "            index=s.index('0')\n",
    "            a=s[:index]\n",
    "            b=s[index:]\n",
    "            for x in a:\n",
    "                if x!='1':\n",
    "                    return False\n",
    "            for x in b:\n",
    "                if x!='0':\n",
    "                    return False\n",
    "        else:\n",
    "            for x in s:\n",
    "                if x!='1':\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        return \"01\" not in s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "     \n",
    "        for i in range(len(s) - 1):\n",
    "            if i + 2 < len(s) and s[i] == '1' and s[i + 1] == '0' and '1' in s[i + 2:]:\n",
    "                return False \n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import re\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        return bool(re.match('(?!<.*1.*)1+(?!.*1.*)',s))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        zero_idx = s.find('0')\n",
    "        \n",
    "        if zero_idx == -1:\n",
    "            return True\n",
    "        else:\n",
    "            return s[zero_idx+1:].find('1') == -1\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        return '01' not in s\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        return '01' not in s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        return sum(int(i) for i,_ in groupby(s)) < 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        flag = False\n",
    "        for c in s:\n",
    "            if c == '0':\n",
    "                flag = True\n",
    "            elif flag:\n",
    "                return False                \n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    \n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        n = len(s) \n",
    "        start, end = 0, n - 1\n",
    "\n",
    "        while end > - 1 and s[end] == '0':\n",
    "            end -= 1\n",
    "\n",
    "        # if end < 0:\n",
    "        #     return False        \n",
    "\n",
    "        while start < n and s[start] == '0':\n",
    "            start += 1\n",
    "\n",
    "        # if start == n:\n",
    "        #     return False\n",
    "\n",
    "\n",
    "        return '0' not in s[start: end + 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        return '1'*s.count('1') in s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        num = 0\n",
    "        flag = False\n",
    "        for i in range(1, len(s)):\n",
    "            if flag and s[i] == '1':\n",
    "                return False\n",
    "            elif not flag and s[i] == '1':\n",
    "                continue\n",
    "            elif not flag and s[i] == '0':\n",
    "                flag = True\n",
    "        return True\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        return \"01\" not in s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        ans = 0\n",
    "\n",
    "        i = 0\n",
    "        while i < len(s):\n",
    "            if s[i] == \"1\":\n",
    "                j = i\n",
    "                while j < len(s):\n",
    "                    if s[j] == \"1\":\n",
    "                        j += 1\n",
    "                    else:\n",
    "                        break\n",
    "                i = j\n",
    "                ans += 1\n",
    "            else:\n",
    "                i += 1\n",
    "        \n",
    "        return ans == 0 or ans == 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        s = s.strip().split('0')\n",
    "        k = 0\n",
    "        for c in s:\n",
    "            if c:\n",
    "                print(c)\n",
    "                k+=1\n",
    "        return k==1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    \n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        n = len(s) \n",
    "        start, end = 0, n - 1\n",
    "\n",
    "        while end > - 1 and s[end] == '0':\n",
    "            end -= 1\n",
    "\n",
    "        if end < 0:\n",
    "            return False        \n",
    "            \n",
    "        return '0' not in s[start: end]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        a={}\n",
    "        for i,j in enumerate(s):\n",
    "            if j=='1' and j not in a:\n",
    "                a[j]=[i]\n",
    "            elif j=='1' and j in a:\n",
    "                a[j].append(i)\n",
    "        if len(a['1'])==1:\n",
    "            return True\n",
    "        else:\n",
    "            if all([a['1'][i]-a['1'][i-1]<2 for i in range(1,len(a['1']))]):\n",
    "                return True\n",
    "            else:\n",
    "                return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        exist = False\n",
    "        for i in s:\n",
    "            if i == '0':\n",
    "                exist = True\n",
    "            if exist and i == '1':\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        n,f=0,0\n",
    "        for i in s:\n",
    "            if f==0 and i=='1':\n",
    "                f=1\n",
    "                n+=1\n",
    "            elif i=='0':\n",
    "                f=0\n",
    "        return n<=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    \n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        n = len(s) \n",
    "        start, end = 0, n - 1\n",
    "        pre, bck = '', ''\n",
    "\n",
    "\n",
    "        while end > - 1 and s[end] == '0':\n",
    "            end -= 1\n",
    "\n",
    "        if end < 0:\n",
    "            return False        \n",
    "\n",
    "        while start < n and s[start] == '0':\n",
    "            start += 1\n",
    "\n",
    "        if start == n:\n",
    "            return False\n",
    "\n",
    "\n",
    "        return '0' not in s[start: end + 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        return \"01\" not in s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        cnt = 0\n",
    "        flag = 1\n",
    "        for i in s:\n",
    "            if i == '1':\n",
    "                cnt += flag \n",
    "                flag = 0\n",
    "            else:\n",
    "                flag = 1\n",
    "        return cnt in (0, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkOnesSegment(self, s: str) -> bool:\n",
    "        i = ans = 0\n",
    "        n = len(s)\n",
    "        while i < n:\n",
    "            if s[i] == \"0\":\n",
    "                i += 1\n",
    "                continue\n",
    "            while i < n and int(s[i]):\n",
    "                i += 1\n",
    "            ans += 1\n",
    "        return ans <= 1"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
